package psi.common;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;

/**
 * 用户是否登录 - 拦截器
 * 
 * @author 李静波
 *
 */
public class AuthInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();

		String uri = JFinal.me().getServletContext().getContextPath();
		if (uri == null || uri.equals("")) {
			uri = "/";
		}
		controller.setAttr("uri", uri);

		String loginUserId = controller.getSessionAttr("loginUserId");
		String loginActionKey = "/Home/User/login";
		if (loginUserId != null)
			if (inv.getActionKey().equals(loginActionKey)) {
				// 已经登录了，就不用再登录了
				controller.redirect("/");
			} else {
				inv.invoke();
			}
		else {
			if (inv.getActionKey().equals(loginActionKey) || inv.getActionKey().equals("/Home/User/loginPOST")) {
				inv.invoke();
			} else {
				controller.redirect(loginActionKey);
			}
		}
	}
}